package com.ym.medical.service.impl;

import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.medical.dao.UserDao;
import com.ym.medical.dao.UserPatientViewDao;
import com.ym.medical.dao.WxUserDao;
import com.ym.medical.dao.YunxinUserDao;
import com.ym.medical.domain.entity.UserPatientViewEntity;
import com.ym.medical.domain.param.UserPatientViewQueryParam;
import com.ym.medical.domain.vo.UserPatientViewListPcVO;
import com.ym.medical.service.IUserPatientViewSV;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 患者用户表Service业务层处理
 *
 * @author ym
 * @date 2020-08-18
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserPatientViewSVImpl extends BaseSVImpl<UserPatientViewEntity, UserPatientViewDao> implements IUserPatientViewSV {

	@Autowired
	private WxUserDao wxUserDao;

	@Autowired
	private UserDao userDao;

	@Autowired
	private YunxinUserDao yunxinUserDao;


	/**
	 * 查询患者用户表列表
	 *
	 * @param queryParam 患者用户表 查询参数
	 * @return 患者用户表
	 */
	@Override
	public List<UserPatientViewListPcVO> selectListPC(UserPatientViewQueryParam queryParam) {
		List<UserPatientViewListPcVO> userPatientViews = mapper.selectListPC(queryParam);
		return userPatientViews;
	}

	/**
	 * 由于事务问题，查不到视图,这里主动查表
	 *
	 * @param unionId
	 * @param openIdMa     小程序ID
	 * @param openIdMp     公众号Id
	 * @param userUserType 用户类型
	 * @return
	 */
	@Override
	public UserPatientViewEntity selectByWxId(String unionId, String openIdMa, String openIdMp) {
		UserPatientViewEntity userPatientViewEntity = this.selectOne(
				SelectWhere()
						.orEqualTo(UserPatientViewEntity::getUnionid, unionId)
						.orEqualTo(UserPatientViewEntity::getOpenidMini, openIdMa)
						.orEqualTo(UserPatientViewEntity::getOpenidMini, openIdMp)
						.end()
		);
		// 如果由于事务原因查不到,尝试主动查表
		if (userPatientViewEntity == null) {
			userPatientViewEntity = mapper.selectByWxId(unionId, openIdMa, openIdMp);
		}
		return userPatientViewEntity;
	}
}
